package com.ymatou.iapi.optrade.testcase;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.AuthenticateFeeTypeEnum;
import com.ymatou.iapi.optrade.parameter.enums.AuthenticateFreightTypeEnum;
import com.ymatou.iapi.optrade.parameter.enums.BizOperateLogEnum;
import com.ymatou.iapi.optrade.parameter.req.ChangeOrderDiscountReq;
import com.ymatou.iapi.optrade.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.optrade.parameter.utill.Utils;
import com.ymatou.iapi.optrade.parameter.vo.BizVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderGroupVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderItemVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderVo;
import com.ymatou.iapi.optrade.resp.Order;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.ChangeOrderDiscountCall;
import com.ymatou.iapi.optrade.service.PrepaidCardService;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceAnliOrderCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceAuthenticateCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.business.service.YMTTrading.TradingProductVo;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.database.sharding.db.sharding.model.OrderBizLogPo;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailPo;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailSubPo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 修改订单折扣和运费(加价或减价) FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ChangeOrderDiscount {

	private static ChangeOrderDiscountReq changeOrderDiscountReq;
	private static ChangeOrderDiscountCall changeorderdiscountcall;
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static Long sellerId = Long.valueOf(EnvSetup.getData("autotest_seller1"));
	private static String sellerName = String.valueOf(EnvSetup.getData("autotest_sellerName1"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修改订单折扣和运费");
	}

	@Before
	public void caseUp() {

		changeOrderDiscountReq = new ChangeOrderDiscountReq();
		changeorderdiscountcall = new ChangeOrderDiscountCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(changeorderdiscountcall.getOpurl());
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 一次下单同 一个商家 2个订单号,不使用优惠券,每个订单下面3个商品
	 * @param isFreightFree 是否包邮
	 * @return
	 */
	private static TradingServiceResp placeOrder6(boolean isFreightFree) {

		PlaceOrderReq placeOrderReq = TradingCallService
				.buildStandPlaceOrderReq();

		//买手1 订单组
		OrderGroupVo groupVo1 = TradingCallService.createOrderGroupVo(sellerId, sellerName);
		OrderVo orderVo1 = TradingCallService.createOrderVo();

		TradingProductVo productVo1 = new TradingProductVo(TestDataManager.getRandomUUID().toString(),
				TestDataManager.getRandomUUID().toString());
		TradingProductVo productVo2 = new TradingProductVo(TestDataManager.getRandomUUID().toString(),
				TestDataManager.getRandomUUID().toString());
		TradingProductVo productVo3 = new TradingProductVo(TestDataManager.getRandomUUID().toString(),
				TestDataManager.getRandomUUID().toString());
		
		OrderItemVo orderItemVo1 = TradingCallService.createOrderItemVo(
				productVo1.getProductId(), productVo1.getCatalogId());
		OrderItemVo orderItemVo2 = TradingCallService.createOrderItemVo(
				productVo2.getProductId(), productVo2.getCatalogId());
		OrderItemVo orderItemVo3 = TradingCallService.createOrderItemVo(
				productVo3.getProductId(), productVo3.getCatalogId());
		
		orderVo1.setOrderItems(Arrays.asList(orderItemVo1, orderItemVo2, orderItemVo3));

		OrderVo orderVo2 = TradingCallService.createOrderVo();

		TradingProductVo productVo4 = new TradingProductVo(TestDataManager.getRandomUUID().toString(),
				TestDataManager.getRandomUUID().toString());
		TradingProductVo productVo5 = new TradingProductVo(TestDataManager.getRandomUUID().toString(),
				TestDataManager.getRandomUUID().toString());
		TradingProductVo productVo6 = new TradingProductVo(TestDataManager.getRandomUUID().toString(),
				TestDataManager.getRandomUUID().toString());
		
		OrderItemVo orderItemVo4 = TradingCallService.createOrderItemVo(
				productVo4.getProductId(), productVo4.getCatalogId());
		OrderItemVo orderItemVo5 = TradingCallService.createOrderItemVo(
				productVo5.getProductId(), productVo5.getCatalogId());
		OrderItemVo orderItemVo6 = TradingCallService.createOrderItemVo(
				productVo6.getProductId(), productVo6.getCatalogId());
		
		orderVo2.setOrderItems(Arrays.asList(orderItemVo4, orderItemVo5,
				orderItemVo6));

		groupVo1.setOrders(Arrays.asList(orderVo1, orderVo2));

		if (isFreightFree) {

			for (OrderVo orderVo : groupVo1.getOrders()) {

				orderVo.getOrderItems().forEach(
						item -> item.setFreight(BigDecimal.ZERO));
			}
		}
		
		// 设置为砍价团订单,不需要交易扣库存，随意设置商品和规格id
		BizVo biz = new BizVo();

		biz.setBizId(TestDataManager.getRandomNum(9));
		biz.setBizSubType(1);
		biz.setBizType(1);

		placeOrderReq.setBizSource(biz);

		return TradingCallService.callPlaceOrderService(placeOrderReq);
	}
	/**
	 * 
	 * 一次下单同 一个商家 2个订单号,不使用优惠券,每个订单下面3个商品,商品不包邮,第二个商品运费为0
	 * 
	 */
	private static TradingServiceResp placeOrder8() {

		PlaceOrderReq placeOrderReq = TradingCallService
				.buildStandPlaceOrderReq();

		// 买手1 订单组
		OrderGroupVo groupVo1 = TradingCallService.createOrderGroupVo(sellerId,
				sellerName);
		OrderVo orderVo1 = TradingCallService.createOrderVo();

		TradingProductVo productVo1 = new TradingProductVo(TestDataManager
				.getRandomUUID().toString(), TestDataManager.getRandomUUID()
				.toString());
		TradingProductVo productVo2 = new TradingProductVo(TestDataManager
				.getRandomUUID().toString(), TestDataManager.getRandomUUID()
				.toString());
		TradingProductVo productVo3 = new TradingProductVo(TestDataManager
				.getRandomUUID().toString(), TestDataManager.getRandomUUID()
				.toString());

		OrderItemVo orderItemVo1 = TradingCallService.createOrderItemVo(
				productVo1.getProductId(), productVo1.getCatalogId());
		OrderItemVo orderItemVo2 = TradingCallService.createOrderItemVo(
				productVo2.getProductId(), productVo2.getCatalogId());
		OrderItemVo orderItemVo3 = TradingCallService.createOrderItemVo(
				productVo3.getProductId(), productVo3.getCatalogId());

		orderVo1.setOrderItems(Arrays.asList(orderItemVo1, orderItemVo2,
				orderItemVo3));

		OrderVo orderVo2 = TradingCallService.createOrderVo();

		TradingProductVo productVo4 = new TradingProductVo(TestDataManager
				.getRandomUUID().toString(), TestDataManager.getRandomUUID()
				.toString());
		TradingProductVo productVo5 = new TradingProductVo(TestDataManager
				.getRandomUUID().toString(), TestDataManager.getRandomUUID()
				.toString());
		TradingProductVo productVo6 = new TradingProductVo(TestDataManager
				.getRandomUUID().toString(), TestDataManager.getRandomUUID()
				.toString());

		OrderItemVo orderItemVo4 = TradingCallService.createOrderItemVo(
				productVo4.getProductId(), productVo4.getCatalogId());
		OrderItemVo orderItemVo5 = TradingCallService.createOrderItemVo(
				productVo5.getProductId(), productVo5.getCatalogId());
		OrderItemVo orderItemVo6 = TradingCallService.createOrderItemVo(
				productVo6.getProductId(), productVo6.getCatalogId());

		orderVo2.setOrderItems(Arrays.asList(orderItemVo4, orderItemVo5,
				orderItemVo6));

		groupVo1.setOrders(Arrays.asList(orderVo1, orderVo2));

		for (OrderVo orderVo : groupVo1.getOrders()) {

			orderVo.getOrderItems().get(1).setFreight(BigDecimal.ZERO);
		}
		
		// 设置为砍价团订单,不需要交易扣库存，随意设置商品和规格id
		BizVo biz = new BizVo();

		biz.setBizId(TestDataManager.getRandomNum(9));
		biz.setBizSubType(1);
		biz.setBizType(1);

		placeOrderReq.setBizSource(biz);

		return TradingCallService.callPlaceOrderService(placeOrderReq);
	}

	/**	
	 * 组合商品
	 * @throws InterruptedException 
	 */
	private static TradingServiceResp placeOrder9() throws Exception {

		return Ts_OrderPay.placeOrder();

	}
	
	private void verifyOrderDetailDb(Order order, boolean hasSubPro) {
		this.verifyOrderDetailDb(order, hasSubPro, null);
	}
	
	/**
	 * 校验订单商品表(order_detail)
	 * @param orderId
	 * @param hasSubPro 是否含有组合商品
	 */
	private void verifyOrderDetailDb(Order order, boolean hasSubPro, String address) {

		long orderId = order.getOrderId();

		BigDecimal oldFreight = order.getOrderPo().getFreight();

		OrderPo actualOrderPo = orderShardingWapper
				.selectOrderbyOrderId(orderId);

		Logger.comment(String.format("校验订单商品表(order) orderId:%s",
				actualOrderPo.getOrderId()));

		DataUtil.verifyDecimalEquals(changeOrderDiscountReq.getFreight(),
				actualOrderPo.getFreight(), "验证数据库Freight");

		DataUtil.verifyDecimalEquals(changeOrderDiscountReq.getDiscount(),
				actualOrderPo.getOrderDiscount(), "验证数据库orderDiscount");

		DataUtil.verifyDecimalEquals(
				actualOrderPo
						.getTotalPrice()
						.add(actualOrderPo.getFreight())
						.add(actualOrderPo.getOrderDiscount())
						.subtract(actualOrderPo.getCardAmount())
						.add(Utils.zeroIfNull(actualOrderPo.getTaxFee()))
						.add(Utils.zeroIfNull(actualOrderPo
								.getUserAuthenticateFee()))
						.add(Utils.falseIfNull(actualOrderPo
								.getAuthenticateFlag())
								&& Utils.zeroIfNull(actualOrderPo
										.getAuthenticateFreightType()) == 2 ? Utils
								.zeroIfNull(actualOrderPo
										.getAuthenticateFreight())
								: BigDecimal.ZERO), actualOrderPo
						.getPayableAmount(), "验证数据库payableAmount");

		List<OrderDetailPo> actualOrderDetails = orderShardingWapper
				.selectOrderDetailbyOrderId(orderId);

		BigDecimal[] weightPro = new BigDecimal[actualOrderDetails.size()];

		BigDecimal[] weight = new BigDecimal[actualOrderDetails.size()];
		
//		boolean isShenqiOrder = actualOrderPo.getGoodsOwnerId() == 0 ? false : true;

		// 码头订单按商品成交价分摊运费
		for (int j = 0; j < actualOrderDetails.size(); j++) {

			OrderDetailPo orderDetailPo = actualOrderDetails.get(j);

//			if (isShenqiOrder)
//				weightPro[j] = orderDetailPo.getProductPrice().multiply(
//						new BigDecimal(orderDetailPo.getProductCount()));
//			else
//				weightPro[j] = orderDetailPo.getOriginalPrice().multiply(
//						new BigDecimal(orderDetailPo.getProductCount()));
			
			weightPro[j] = orderDetailPo.getProductPrice().multiply(
					new BigDecimal(orderDetailPo.getProductCount()));

			weight[j] = weightPro[j];
		}

		//原路的运费大于0，按订单商品上的运费比例分摊 新运费
		if (Utils.isBiggerThan(oldFreight, BigDecimal.ZERO)) {

			for (int j = 0; j < order.getOrderDetailPoList().size(); j++) {

				OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(
						j);

				weight[j] = orderDetailPo.getFreight();
			}

		}

		BigDecimal discount = changeOrderDiscountReq.getDiscount();
		BigDecimal freight = changeOrderDiscountReq.getFreight();

		BigDecimal proDiscount[] = Utils.apportionByWeights3(discount,
				weightPro, null);

		BigDecimal proFreight[] = Utils.apportionByWeights3(freight, weight, null);

		for (int i = 0; i < actualOrderDetails.size(); i++) {

			OrderDetailPo actualOrderDetail = actualOrderDetails.get(i);

			Logger.comment(String.format(
					"校验订单商品表(order_detail) orderId:%s,OrderDetailId:%s",
					actualOrderDetail.getOrderId(),
					actualOrderDetail.getOrderDetailId()));

			DataUtil.verifyDecimalEquals(proFreight[i],
					actualOrderDetail.getFreight(), "验证数据库Freight");

			Logger.verifyEquals(
					true,
					Utils.calcTimeSpanMin(actualOrderDetail.getUpdateTime()) < 2,
					"验证数据库UpdateTime跟系统当前时间差为2分钟内");

			DataUtil.verifyDecimalEquals(proDiscount[i],
					actualOrderDetail.getDiscount(), "验证数据库Discount");
			
			int scale = 2;
			
			if (null == actualOrderPo.getCurrency()) 
				scale = 2;
			else if (null != actualOrderPo.getCurrency() && actualOrderPo.getCurrency().equalsIgnoreCase("JPY"))
				scale = 0;
			
			boolean fullReductionFreeFreight = Utils.falseIfNull(actualOrderDetail.getFullReductionFreeFreight());
			BigDecimal taxFee = null;
			
			if (SqTradingCallService.isFreePerimeters(Utils.emptyIfNull(address)))
				taxFee = BigDecimal.ZERO;
			else
				taxFee = Utils
						.zeroIfNull(
								fullReductionFreeFreight ? BigDecimal.ZERO
										: actualOrderDetail.getFreight())
						.divide(new BigDecimal(
								actualOrderDetail.getProductCount()), 32,
								RoundingMode.DOWN)
						.add(actualOrderDetail.getProductPrice())
						.add(Utils.zeroIfNull(actualOrderDetail.getDiscount())
								.divide(new BigDecimal(actualOrderDetail
										.getProductCount()), 32,
										RoundingMode.DOWN))
						.subtract(
								Utils.zeroIfNull(actualOrderDetail
										.getYmtCouponAmount()))
						.subtract(
								Utils.zeroIfNull(actualOrderDetail
										.getYmtPromotionAmount()))
						.subtract(
								Utils.zeroIfNull(actualOrderDetail
										.getSellerCouponAmount()))
						.subtract(
								Utils.zeroIfNull(actualOrderDetail
										.getPromotionAmount()))
						.multiply(actualOrderDetail.getTaxRate())
						.setScale(scale, RoundingMode.HALF_DOWN);
			
			DataUtil.verifyDecimalEquals(taxFee, actualOrderDetail.getTaxFee(), "验证数据库TaxFee");

			//订单商品是 套装商品
			if (hasSubPro) {

				//校验订单商品扩展表(order_detail_ext)
				verifyOrderDetailSubDb(actualOrderDetail);
			}
		}
		
		//订单主状态操作日志表(order_biz_log)
		OrderBizLogPo orderBizLogPo = orderShardingWapper
				.selectOrderBizLogByOrderId(orderId);

		Logger.comment("验证订单业务操作记录表(order_biz_log)");

		String logContent = String.format(
				"运费:%s(old),折扣:%s(old); 运费:%s(new),折扣:%s(new),客服:null", oldFreight,
				order.getOrderPo().getOrderDiscount(),
				changeOrderDiscountReq.getFreight(),
				changeOrderDiscountReq.getDiscount());

		Logger.verifyEquals(logContent, orderBizLogPo.getContent(),
				"验证数据库Content");

		Logger.verifyEquals(orderId, orderBizLogPo.getOrderId(), "验证数据库OrderId");

		Logger.verifyEquals(String.valueOf(orderId), orderBizLogPo.getBizNo(),
				"验证数据库BizNo");
		Logger.verifyEquals(
				BizOperateLogEnum.CHANGE_DISCOUNT_FREIGHT_INFO.toString(),
				orderBizLogPo.getBizType(), "验证数据库BizType");
		Logger.verifyEquals(changeOrderDiscountReq.getSellerId(),
				orderBizLogPo.getOperator(), "验证数据库Operator");
	}
	
	/**
	 * 校验订单组合子商品表（order_detail_sub）
	 * @param expectedOrderDetails
	 * @param actualOrderDetails
	 */
	private void verifyOrderDetailSubDb(OrderDetailPo actualOrderDetail) {

		long orderDetailId = actualOrderDetail.getOrderDetailId();
		long orderId = actualOrderDetail.getOrderId();

		List<OrderDetailSubPo> subPoList = orderShardingWapper
				.selectOrderDetailSubByDetailId(orderDetailId);

		String catalogId = actualOrderDetail.getCatalogId();

		Logger.comment(String.format(
				"校验订单组合子商品表（order_detail_sub） orderId:%s,catalogId:%s",
				actualOrderDetail.getOrderId(), catalogId));

		BigDecimal[] weight = new BigDecimal[subPoList.size()];

		for (int j = 0; j < subPoList.size(); j++) {

			OrderDetailSubPo orderInfoSubPo = subPoList.get(j);

			weight[j] = orderInfoSubPo.getOriginalPrice().multiply(
					new BigDecimal(orderInfoSubPo.getProductCount()));
		}

		BigDecimal freight[] = Utils.apportionByWeights3(
				actualOrderDetail.getFreight(), weight, null);
		BigDecimal subDiscount[] = Utils.apportionByWeights3(
				actualOrderDetail.getDiscount(), weight, null);

		int k = 0;

		for (OrderDetailSubPo actualOrderDetailSub : subPoList) {

			Logger.comment(String.format("套装子商品序号 %s",
					actualOrderDetailSub.getId()));

			Logger.verifyEquals(orderId, actualOrderDetailSub.getOrderId(),
					"验证数据库OrderId");

			Logger.verifyEquals(freight[k], actualOrderDetailSub.getFreight(),
					"验证数据库Freight");

			Logger.verifyEquals(
					true,
					Utils.calcTimeSpanMin(actualOrderDetailSub.getUpdateTime()) < 2,
					"验证数据库UpdateTime跟系统当前时间差为2分钟内");

			DataUtil.verifyDecimalEquals(subDiscount[k],
					actualOrderDetailSub.getDiscount(), "验证数据库Discount");

			k++;

		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_001() {
		Logger.start(true, "订单下面有多个商品,修改折扣为涨价,校验付款后调整金额的分摊");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = placeOrder6(false);

			long userId = placeOrderResp.getUserId();

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			long sellerId = order.getSellerId();

			long tradingId = placeOrderResp.getTradingId();

			// 参数
			BigDecimal discount = new BigDecimal(17);
			changeOrderDiscountReq.setDiscount(discount);

			BigDecimal freight = new BigDecimal(38);

			changeOrderDiscountReq.setFreight(freight);
			changeOrderDiscountReq.setOrderId(orderId);
			changeOrderDiscountReq.setSellerId(sellerId);

			// 发送请求
			changeorderdiscountcall.setData(changeOrderDiscountReq);
			changeorderdiscountcall.callService();

			// 付款
			TradingCallService.payOrder(userId,
					TradingCallService.getPayableAmount(tradingId), tradingId);

			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");

			// 数据库验证 
			verifyOrderDetailDb(order, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_002() {
		Logger.start(true, "订单下面有多个商品,修改折扣为降价,校验付款后调整金额的分摊");
		try {

			TradingServiceResp placeOrderResp = placeOrder6(false);

			long userId = placeOrderResp.getUserId();

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			long sellerId = order.getSellerId();

			long tradingId = placeOrderResp.getTradingId();

			// 参数
			BigDecimal discount = new BigDecimal(-18);
			changeOrderDiscountReq.setDiscount(discount);

			BigDecimal freight = new BigDecimal(38);
			changeOrderDiscountReq.setFreight(freight);
			changeOrderDiscountReq.setOrderId(orderId);
			changeOrderDiscountReq.setSellerId(sellerId);
			// 发送请求
			changeorderdiscountcall.setData(changeOrderDiscountReq);
			changeorderdiscountcall.callService();

			// 付款
			TradingCallService.payOrder(userId,
					TradingCallService.getPayableAmount(tradingId), tradingId);

			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");

			// 数据库验证 
			verifyOrderDetailDb(order, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_003() {
		Logger.start(true, "订单下多个商品原有运费,调整运费为0,校验运费分摊到商品逻辑");
		try {

			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = placeOrder6(false);

			long userId = placeOrderResp.getUserId();

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			long sellerId = order.getSellerId();

			long tradingId = placeOrderResp.getTradingId();

			// 参数
			BigDecimal discount = new BigDecimal(28);

			BigDecimal freight = BigDecimal.ZERO;

			changeOrderDiscountReq.setDiscount(discount);
			changeOrderDiscountReq.setFreight(freight);
			changeOrderDiscountReq.setOrderId(orderId);
			changeOrderDiscountReq.setSellerId(sellerId);
			// 发送请求
			changeorderdiscountcall.setData(changeOrderDiscountReq);
			changeorderdiscountcall.callService();

			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");

			// 付款
			TradingCallService.payOrder(userId,
					TradingCallService.getPayableAmount(tradingId), tradingId);

			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");

			// 数据库验证 
			verifyOrderDetailDb(order, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_004() {
		Logger.start(true, "订单下面有多个商品,订单为包邮,调整为不包邮,校验付款后调整金额的分摊");
		try {

			TradingServiceResp placeOrderResp = placeOrder6(true);

			long userId = placeOrderResp.getUserId();

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			long sellerId = order.getSellerId();

			long tradingId = placeOrderResp.getTradingId();

			// 参数
			BigDecimal discount = new BigDecimal(-18);
			changeOrderDiscountReq.setDiscount(discount);

			BigDecimal freight = new BigDecimal(38);
			changeOrderDiscountReq.setFreight(freight);
			changeOrderDiscountReq.setOrderId(orderId);
			changeOrderDiscountReq.setSellerId(sellerId);
			// 发送请求
			changeorderdiscountcall.setData(changeOrderDiscountReq);
			changeorderdiscountcall.callService();

			// 付款
			TradingCallService.payOrder(userId,
					TradingCallService.getPayableAmount(tradingId), tradingId);

			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");

			// 数据库验证 
			verifyOrderDetailDb(order, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_005() {
		Logger.start(true, "订单下多个商品原有运费其中有商品运费为0,调整运费,校验运费分摊到商品逻辑");
		try {

			// 创建订单
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrderResp = placeOrder8();

			long userId = placeOrderResp.getUserId();

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			long sellerId = order.getSellerId();

			long tradingId = placeOrderResp.getTradingId();

			// 参数
			BigDecimal discount = new BigDecimal(28);
			changeOrderDiscountReq.setDiscount(discount);

			BigDecimal freight = new BigDecimal(60);

			changeOrderDiscountReq.setFreight(freight);
			changeOrderDiscountReq.setOrderId(orderId);
			changeOrderDiscountReq.setSellerId(sellerId);
			// 发送请求
			changeorderdiscountcall.setData(changeOrderDiscountReq);
			changeorderdiscountcall.callService();

			// 付款
			TradingCallService.payOrder(userId,
					TradingCallService.getPayableAmount(tradingId), tradingId);

			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");

			// 数据库验证
			verifyOrderDetailDb(order, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_006() {
		Logger.start(true, "订单组合商品,调整运费,折扣");
		try {

			// 创建订单
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrderResp = placeOrder9();

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();

			long orderId = order.getOrderId();
			
			TradingSyncService.getSyncResult(orderId);

			// 参数
			BigDecimal discount = new BigDecimal(28);
			changeOrderDiscountReq.setDiscount(discount);

			changeOrderDiscountReq.setFreight(new BigDecimal(30));
			changeOrderDiscountReq.setOrderId(orderId);
			changeOrderDiscountReq.setSellerId(sellerId);

			// 发送请求
			changeorderdiscountcall.setData(changeOrderDiscountReq);
			changeorderdiscountcall.callService();
			
			TradingSyncService.getSyncResult(orderId);

			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");

			// 数据库验证
			verifyOrderDetailDb(order, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_007() {
		Logger.start(true, "使用了预付卡的订单,调整运费,折扣");
		try {

			// 创建订单
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrderResp = PrepaidCardService
					.placeOrder2(false);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();

			long orderId = order.getOrderId();

			order.getOrderPayableAmount();

			// 参数
			BigDecimal discount = new BigDecimal(-10);
			changeOrderDiscountReq.setDiscount(discount);

			changeOrderDiscountReq.setFreight(new BigDecimal(30));
			changeOrderDiscountReq.setOrderId(orderId);
			changeOrderDiscountReq.setSellerId(sellerId);
			// 发送请求
			changeorderdiscountcall.setData(changeOrderDiscountReq);
			changeorderdiscountcall.callService();

			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");
			// 数据库验证

			// 订单主表
			verifyOrderDetailDb(order, false);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_008() {
		Logger.start(true, "订单号不存在,修改订单折扣和运费失败");
		try {
			// 参数
			long orderId = 1334;
			long sellerId = 288484;
			BigDecimal discount = new BigDecimal(1);
			changeOrderDiscountReq.setDiscount(discount);

			BigDecimal freight = new BigDecimal(5);
			changeOrderDiscountReq.setFreight(freight);
			changeOrderDiscountReq.setOrderId(orderId);
			changeOrderDiscountReq.setSellerId(sellerId);
			// 发送请求
			changeorderdiscountcall.setData(changeOrderDiscountReq);
			changeorderdiscountcall.callService();
			
			Logger.verifyEquals(ECode.FAILED201,
					changeorderdiscountcall.getCode(), "验证Code");
			Logger.verifyIsContains(ECode.FAILED201_MSG_11,
					changeorderdiscountcall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_009() {
		Logger.start(true, "当前订单状态不能修改价格");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrderResp = TradingCallService.payOrder();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			Logger.comment("前置数据准备结束");
			// 参数
			BigDecimal discount = new BigDecimal(1);
			changeOrderDiscountReq.setDiscount(discount);

			BigDecimal freight = new BigDecimal(5);
			changeOrderDiscountReq.setFreight(freight);
			changeOrderDiscountReq.setOrderId(orderId);
			changeOrderDiscountReq.setSellerId(sellerId);
			// 发送请求
			changeorderdiscountcall.setData(changeOrderDiscountReq);
			changeorderdiscountcall.callService();
			
			Logger.verifyEquals(ECode.FAILED201,
					changeorderdiscountcall.getCode(), "验证Code");
			Logger.verifyEquals("102", changeorderdiscountcall.getBCode(),
					"验证bcode");
			Logger.verifyEquals("当前订单不能修改价格", changeorderdiscountcall.getMsg(),
					"验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_010() {
		Logger.start(true, "当前订单使用了优惠券不可修改价格");
		try {
			// 创建订单

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService.placeOrderWithCoupon();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			// 参数
			BigDecimal Discount = new BigDecimal(1);
			changeOrderDiscountReq.setDiscount(Discount);

			BigDecimal Freight = new BigDecimal(5);
			changeOrderDiscountReq.setFreight(Freight);
			changeOrderDiscountReq.setOrderId(orderId);
			changeOrderDiscountReq.setSellerId(sellerId);

			// 发送请求
			changeorderdiscountcall.setData(changeOrderDiscountReq);
			changeorderdiscountcall.callService();
			
			Logger.verifyEquals(ECode.FAILED201,
					changeorderdiscountcall.getCode(), "验证Code");
			Logger.verifyEquals(ECode.FAILED201_MSG_46,
					changeorderdiscountcall.getMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_011() {
		Logger.start(false, "订单号为null");
		try {
			// 参数
			BigDecimal Discount = new BigDecimal(1);
			changeOrderDiscountReq.setDiscount(Discount);

			BigDecimal Freight = new BigDecimal(5);
			changeOrderDiscountReq.setFreight(Freight);
			// changeOrderDiscountReq.setOrderId(orderId);
			changeOrderDiscountReq.setSellerId(111);
			// 发送请求
			changeorderdiscountcall.setData(changeOrderDiscountReq);
			changeorderdiscountcall.callService();
			
			Logger.verifyEquals(ECode.ERROR400,
					changeorderdiscountcall.getCode(), "验证Code");
			Logger.verifyIsContains("订单号不能为空",
					changeorderdiscountcall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_012() {
		Logger.start(false, "操作用户Id为null");
		try {
			// 参数
			BigDecimal Discount = new BigDecimal(1);
			changeOrderDiscountReq.setDiscount(Discount);

			BigDecimal Freight = new BigDecimal(5);
			changeOrderDiscountReq.setFreight(Freight);
			changeOrderDiscountReq.setOrderId(1111);
			// changeOrderDiscountReq.setSellerId(111);
			// 发送请求
			changeorderdiscountcall.setData(changeOrderDiscountReq);
			changeorderdiscountcall.callService();
			
			Logger.verifyEquals(ECode.ERROR400,
					changeorderdiscountcall.getCode(), "验证Code");
			Logger.verifyEquals("sellerId:买手ID未提供",
					changeorderdiscountcall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_013() {
		Logger.start(false, "运费小于0");
		try {
			// 参数
			BigDecimal Discount = new BigDecimal(1);
			changeOrderDiscountReq.setDiscount(Discount);

			BigDecimal Freight = new BigDecimal(-1);
			changeOrderDiscountReq.setFreight(Freight);
			changeOrderDiscountReq.setOrderId(1111);
			changeOrderDiscountReq.setSellerId(111);

			// 发送请求
			changeorderdiscountcall.setData(changeOrderDiscountReq);
			changeorderdiscountcall.callService();
			
			Logger.verifyEquals(ECode.ERROR400,
					changeorderdiscountcall.getCode(), "验证Code");
			Logger.verifyEquals("freight:" + ECode.FAILED201_MSG_71,
					changeorderdiscountcall.getMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_014() {
		Logger.start(true, "调整金额过大，导致订单付款金额为0");
		try {
			// 创建订单

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLuxury(false);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			BigDecimal payableAmount = placeOrderResp.getPayableAmount();
			// 参数
			BigDecimal Discount = new BigDecimal(-1).multiply(payableAmount);

			changeOrderDiscountReq.setDiscount(Discount);

			BigDecimal Freight = new BigDecimal(5);

			changeOrderDiscountReq.setFreight(Freight);
			changeOrderDiscountReq.setOrderId(orderId);
			changeOrderDiscountReq.setSellerId(sellerId);

			// 发送请求
			changeorderdiscountcall.setData(changeOrderDiscountReq);
			changeorderdiscountcall.callService();

			Logger.verifyEquals(ECode.FAILED201,
					changeorderdiscountcall.getCode(), "验证Code");

			Logger.verifyEquals("114", changeorderdiscountcall.getBCode(),
					"验证bcode");

			Logger.verifyEquals("调整后订单应付金额必须大于0", changeorderdiscountcall.getMsg(),
					"验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_015() {
		Logger.start(true, "当前订单里含有平台优惠的商品,不可修改价格");
		try {
			// 创建订单

			PlaceOrderReq placeOrderReq = TradingCallService
					.buildStandPlaceOrderReq();
			
			OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
			OrderVo orderVo = TradingCallService.createOrderVo();
			
			// 创建一个普通商品
			TradingProductVo product = TradingProductVo.createPro(sellerId,
					sellerName, 100);
			
			OrderItemVo orderItemVo = TradingCallService.createOrderItemVo(
					product.getProductId(), product.getCatalogId());
			orderItemVo.setYmtPromotionAmount(BigDecimal.TEN);
			
			orderVo.setOrderItems(Arrays.asList(orderItemVo));
			orderGroupVo.setOrders(Arrays.asList(orderVo));
			placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));

			TradingServiceResp placeOrderResp = TradingCallService
					.callPlaceOrderService(placeOrderReq);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			// 参数
			BigDecimal Discount = new BigDecimal(1);
			changeOrderDiscountReq.setDiscount(Discount);

			BigDecimal Freight = new BigDecimal(5);
			changeOrderDiscountReq.setFreight(Freight);
			changeOrderDiscountReq.setOrderId(orderId);
			changeOrderDiscountReq.setSellerId(sellerId);

			// 发送请求
			changeorderdiscountcall.setData(changeOrderDiscountReq);
			changeorderdiscountcall.callService();
			
			Logger.verifyEquals(ECode.FAILED201,
					changeorderdiscountcall.getCode(), "验证Code");
			Logger.verifyEquals(ECode.FAILED201_MSG_46,
					changeorderdiscountcall.getMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_023() {
		Logger.start(true, "下单使用了返点金额,调价失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingPlaceAnliOrderCallService
					.placeAnlihuiOrder(BigDecimal.TEN, false);

			Order order = placeOrderResp.getOrderList().get(0);
			long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			// 参数
			BigDecimal discount = new BigDecimal(17);
			changeOrderDiscountReq.setDiscount(discount);

			BigDecimal freight = new BigDecimal(38);

			changeOrderDiscountReq.setFreight(freight);
			changeOrderDiscountReq.setOrderId(orderId);
			changeOrderDiscountReq.setSellerId(sellerId);

			// 发送请求
			changeorderdiscountcall.setData(changeOrderDiscountReq);
			changeorderdiscountcall.callService();

			Logger.verifyEquals(ECode.FAILED201,
					changeorderdiscountcall.getCode(), "验证Code");
			Logger.verifyEquals("105", changeorderdiscountcall.getBCode(),
					"验证bcode");
			Logger.verifyEquals("当前订单使用了返点消费,不可修改价格", changeorderdiscountcall.getMsg(),
					"验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_024() {
		Logger.start(true, "订单下面一个商品,修改折扣为降价,买手承担鉴定费和鉴定物流费，校验付款后调整金额的分摊");
		try {

			TradingServiceResp placeOrderResp = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticate(AuthenticateFreightTypeEnum.SELLER,
							BigDecimal.ONE, AuthenticateFeeTypeEnum.SELLER,
							BigDecimal.TEN);

			long userId = placeOrderResp.getUserId();

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			long sellerId = order.getSellerId();

			long tradingId = placeOrderResp.getTradingId();

			// 参数
			BigDecimal discount = new BigDecimal(-18);
			changeOrderDiscountReq.setDiscount(discount);

			BigDecimal freight = new BigDecimal(38);
			changeOrderDiscountReq.setFreight(freight);
			changeOrderDiscountReq.setOrderId(orderId);
			changeOrderDiscountReq.setSellerId(sellerId);
			// 发送请求
			changeorderdiscountcall.setData(changeOrderDiscountReq);
			changeorderdiscountcall.callService();

			// 付款
			TradingCallService.payOrder(userId,
					TradingCallService.getPayableAmount(tradingId), tradingId);

			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");

			// 数据库验证 
			verifyOrderDetailDb(order, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_025() {
		Logger.start(true, "订单下面一个商品,上调价格,买家承担鉴定费和鉴定物流费，校验付款后调整金额的分摊");
		try {

			TradingServiceResp placeOrderResp = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticate(AuthenticateFreightTypeEnum.BUYER,
							BigDecimal.ONE, AuthenticateFeeTypeEnum.BUYER,
							BigDecimal.TEN);

			long userId = placeOrderResp.getUserId();

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			long sellerId = order.getSellerId();

			long tradingId = placeOrderResp.getTradingId();

			// 参数
			BigDecimal discount = new BigDecimal(18);
			changeOrderDiscountReq.setDiscount(discount);

			BigDecimal freight = new BigDecimal(38);
			changeOrderDiscountReq.setFreight(freight);
			changeOrderDiscountReq.setOrderId(orderId);
			changeOrderDiscountReq.setSellerId(sellerId);
			
			// 发送请求
			changeorderdiscountcall.setData(changeOrderDiscountReq);
			changeorderdiscountcall.callService();

			// 付款
			TradingCallService.payOrder(userId,
					TradingCallService.getPayableAmount(tradingId), tradingId);

			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");

			// 数据库验证 
			verifyOrderDetailDb(order, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
